unit SqlFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, FMTBcd, SqlExpr, Provider, DB, DBClient, ActnList, Grids, DBGrids,
  ExtCtrls, StdCtrls, ComCtrls, DBGridA, DBCtrls, Buttons, Menus, SpeedButtonA;

type
  TSqlForm = class(TForm)
    ButtonPanel: TPanel;
    Button1: TButton;
    Memo: TMemo;
    Splitter1: TSplitter;
    Grid: TDBGrid;
    ActionList: TActionList;
    AcRunQuery: TAction;
    DataSource: TDataSource;
    Ds: TClientDataSet;
    Provider: TDataSetProvider;
    Query: TSQLDataSet;
    StatusBar: TStatusBar;
    DBNavigator1: TDBNavigator;
    MenuButton: TSpeedButton;
    PopupMenu: TPopupMenu;
    MCopyToClipboard: TMenuItem;
    procedure AcRunQueryExecute(Sender: TObject);
    procedure MCopyToClipboardClick(Sender: TObject);
  private
    { Private declarations }
  protected
    procedure CreateParams(var Params: TCreateParams); override;

  end;

var
  SqlForm: TSqlForm = nil;

implementation
uses
    ProgramM, DataExport;

{$R *.dfm}

procedure TSqlForm.AcRunQueryExecute(Sender: TObject);
var
    Sql: String;
begin
    Ds.Close;
    Query.CommandText := Memo.Text;
    Sql := Trim(Lowercase(Query.CommandText));
    if (Copy(Sql, 1, 6) = 'select') or (Copy(Sql, 1, 4) = 'with') then
    begin
        Ds.Open;
    end
    else
    begin
        Query.ExecSQL;
    end;
end;

procedure TSqlForm.CreateParams(var Params: TCreateParams);
begin
  inherited;
  Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
  Params.WndParent := GetDesktopwindow;
end;

procedure TSqlForm.MCopyToClipboardClick(Sender: TObject);
begin
  CopyDatasetToClipboard(Ds);
end;

end.
